package nju.bishe.weibo.data.db;

import java.util.ArrayList;
import java.util.List;

import nju.bishe.weibo.data.UserToken;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 用户多用户登陆的用户数据保存，accessToken(token,tokenScret)
 * 
 * @author longwei.wu
 * 
 */
public class UserTokenTable {

	public static final String DEVICE_ALERT_ENABLED_ZIP = "DAEZ99";
	public static final String DB_NAME = "w_alert";
	public static final String DB_TABLE = "w_alert_loc";
	public static final int DB_VERSION = 3;

	private static final String CLASSNAME = UserTokenTable.class.getSimpleName();
	private static final String[] COLS = new String[] { "userId", "token",
			"secret", "userName", "userIcon" };

	private SQLiteDatabase db;
	private static UserTokenHelper dbOpenHelper;

	private static class UserTokenHelper extends SQLiteOpenHelper {

		private static final String DB_CREATE = "CREATE TABLE "
				+ UserTokenTable.DB_TABLE
				+ " (userId INTEGER PRIMARY KEY, token TEXT UNIQUE NOT NULL, secret TEXT NOT NULL, userName TEXT  NULL,userIcon BLOB );";

		public UserTokenHelper(final Context context) {
			super(context, UserTokenTable.DB_NAME, null, UserTokenTable.DB_VERSION);
		}

		@Override
		public void onCreate(final SQLiteDatabase db) {
			try {
				db.execSQL(UserTokenHelper.DB_CREATE);
			} catch (SQLException e) {
			}
		}

		@Override
		public void onOpen(final SQLiteDatabase db) {
			super.onOpen(db);
		}

		@Override
		public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
				final int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + UserTokenTable.DB_TABLE);
			onCreate(db);
		}
	}

	/*
	 * 获得已登录用户的token
	 */
	public List<UserToken> getAllUsersRemember() {

		List<UserToken> users = new ArrayList<UserToken>();
		Cursor c = null;
		try {
			c = this.db.query(UserTokenTable.DB_TABLE, UserTokenTable.COLS, null, null,
					null, null, null);
			int numRows = c.getCount();
			c.moveToFirst();
			for (int i = 0; i < numRows; ++i) {
				UserToken userToken = new UserToken();
				userToken.setUser_id(c.getString(0));
				userToken.setToken(c.getString(1));
				userToken.setSecret(c.getString(2));
				userToken.setUserName(c.getString(3));
				userToken.setIcon(c.getBlob(4));
				users.add(userToken);
				c.moveToNext();
			}
		} catch (SQLException e) {
			Log.v("MyWeibo", UserTokenTable.CLASSNAME, e);
		} finally {
			if (c != null && !c.isClosed()) {
				c.close();
			}
		}
		return users;
	}

	/*
	 * 插入授权用户
	 */

	public void insert(final UserToken userTokenWrapper) {
		ContentValues values = new ContentValues();
		values.put("userId", userTokenWrapper.getUser_id());
		values.put("token", userTokenWrapper.getToken());
		values.put("secret", userTokenWrapper.getSecret());
		values.put("userName", userTokenWrapper.getUserName());
		values.put("userIcon", userTokenWrapper.getIcon());
		this.db.insert(UserTokenTable.DB_TABLE, null, values);
	}

	/*
	 * 删除用户
	 */
	public void delete(final long id) {
		this.db.delete(UserTokenTable.DB_TABLE, "userId=" + id, null);
	}



	public UserTokenTable(final Context context) {
		if (UserTokenTable.dbOpenHelper == null)
			UserTokenTable.dbOpenHelper = new UserTokenHelper(context);
		establishDb();
	}

	private void establishDb() {
		if (this.db == null) {
			this.db = UserTokenTable.dbOpenHelper.getWritableDatabase();
		}
	}

	public void cleanup() {
		if (this.db != null) {
			this.db.close();
			this.db = null;
		}
	}
}
